<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-input v-model="filterText" placeholder="请输入试验类别" clearable size="small" prefix-icon="el-icon-search" />
          <el-tree class="input-tree" :data="options" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="110px">
            <el-form-item label="检测项目名称" prop="qualName">
              <el-input v-model="queryParams.qualName" placeholder="请输入技术指标名称" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="单位" prop="qualUnit">
              <el-input v-model="queryParams.qualUnit" placeholder="请输入单位" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['sycs:biQualification:add']">新增</el-button>
            </el-col>
          </el-row>

          <el-table border stripe v-loading="loading" :data="biQualificationList">
            <el-table-column type="index" label="序号" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="检测项目key" align="center" prop="qualId" width="100" :show-overflow-tooltip="true" />
            <el-table-column label="检测项目名称" align="center" prop="qualName" :show-overflow-tooltip="true" />
            <el-table-column label="单位" align="center" prop="qualUnit" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['sycs:biQualification:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['sycs:biQualification:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改技术指标图鉴对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="所属试验">
          <div style="font-weight: bolder; font-size: 16px;">{{ this.currentNode.label }}</div>
        </el-form-item>
        <el-form-item label="技术指标名称" prop="qualName">
          <el-input v-model="form.qualName" placeholder="请输入技术指标名称" />
        </el-form-item>
        <el-form-item label="单位" prop="qualUnit">
          <el-input v-model="form.qualUnit" placeholder="请输入单位" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBiQualification, getBiQualification, delBiQualification, addBiQualification, updateBiQualification, syTpyeTree } from "@/api/sycs/biQualification";
import { mapGetters } from "vuex";

export default {
  name: "BiQualification",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技术指标图鉴表格数据
      biQualificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qualName: undefined,
        qualUnit: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qualName: [{ required: true, message: "技术指标名称不能为空", trigger: "blur" }],
        qualUnit: [{ required: true, message: "单位不能为空", trigger: "blur" }]
      },
      filterText: undefined,
      options: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 当前树节点
      currentNode: {}
    };
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.getDeptTree();
  },
  methods: {
    /** 查询技术指标图鉴列表 */
    getList () {
      this.loading = true;
      listBiQualification(this.queryParams).then((response) => {
        this.biQualificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询树结构 */
    getDeptTree () {
      let param = {
        innerFlag: 1
      };
      syTpyeTree(param).then((response) => {
        this.options = response.data;
      });
    },
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        qualId: undefined,
        qualName: undefined,
        qualUnit: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      if (!this.currentNode.id) {
        this.$alert("", "请选择试验类型", {
          confirmButtonText: "确定",
          type: "warning",
          center: true
        });
        return;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd () {
      if (!this.currentNode.id) {
        this.$alert("", "请选择试验类型", {
          confirmButtonText: "确定",
          type: "warning",
          center: true
        });
        return;
      }
      this.reset();
      this.open = true;
      this.title = "添加技术指标图鉴";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const qualId = row.qualId || this.ids;
      getBiQualification(qualId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改技术指标图鉴";
      });
    },
    /** 提交按钮 */
    submitForm () {
      if (!this.currentNode.id) {
        this.$alert("", "请选择试验类型", {
          confirmButtonText: "确定",
          type: "warning",
          center: true
        });
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form = {
            ...this.form,
            syType: this.currentNode.id
          };
          this.buttonLoading = true;
          if (this.form.qualId != null) {
            updateBiQualification(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBiQualification(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const qualIds = row.qualId || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(() => {
          this.loading = true;
          return delBiQualification(qualIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.$set(this.currentNode, "id", data.id);
      this.$set(this.currentNode, "label", data.label);
      this.$set(this.currentNode, "weight", data.weight);
      this.queryParams.syType = data.id;
      this.getList();
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/demo.scss";
</style>
